package com.finger.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.finger.commonutils.JwtUtils;
import com.finger.educenter.entity.vo.LoginVO;
import com.finger.educenter.entity.UcenterMember;
import com.finger.educenter.entity.vo.RegisterVO;
import com.finger.educenter.mapper.UcenterMemberMapper;
import com.finger.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.finger.eduservice.exception.GuliException;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author finger
 * @since 2022-01-14
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Override
    public String doLogin(LoginVO member) {
        String loginMobile = member.getMobile();
        String loginPassword = member.getPassword();

        if(StringUtils.isEmpty(loginMobile) || loginMobile.length()!=11){
            throw new GuliException(20001,"登录失败");
        }

        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();

        ucenterMemberQueryWrapper.eq("mobile",loginMobile);

        UcenterMember ucenterMember = baseMapper.selectOne(ucenterMemberQueryWrapper);

        if(ucenterMember==null){
            throw new GuliException(20001,"登录失败");
        }

        if(ucenterMember.getIsDisabled()){
            throw new GuliException(20001,"登录失败");
        }
        String password = ucenterMember.getPassword();

        if(StringUtils.isEmpty(loginPassword) || !loginPassword.equals(password)){
            throw new GuliException(20001,"登录失败");
        }

        String token = JwtUtils.getJwtToken(ucenterMember.getId(), ucenterMember.getNickname());

        return token;
    }

    @Override
    public void register(RegisterVO registerVO) {
        String mobile = registerVO.getMobile();
        String password = registerVO.getPassword();
        String nickname = registerVO.getNickname();
        String code = registerVO.getCode();

        if(StringUtils.isEmpty(password) || mobile.length()!=11
        || StringUtils.isEmpty(nickname)|| StringUtils.isEmpty(code)){
            throw new GuliException(20001,"注册失败");
        }
        String redisCode = redisTemplate.opsForValue().get(mobile);

        if(!Objects.equals(code,redisCode)){
            throw new GuliException(20001,"注册失败");
        }

        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();

        ucenterMemberQueryWrapper.eq("mobile",mobile);

        Integer count = baseMapper.selectCount(ucenterMemberQueryWrapper);

        if(count>0){
            throw new GuliException(20001,"注册失败");
        }

        // 当一切判断完之后移除redis中的验证码
        Boolean isDelete = redisTemplate.delete(mobile);

        if(!isDelete){
            throw new GuliException(20001,"注册失败");
        }

        UcenterMember ucenterMember = new UcenterMember();

        ucenterMember.setMobile(mobile);
        ucenterMember.setPassword(password);
        ucenterMember.setNickname(nickname);
        ucenterMember.setAvatar("http://littlefinger.oss-cn-beijing.aliyuncs.com/2022/01/13/664d80a0-9ba1-4876-ab41-33b01a527e3bfile.png");
        ucenterMember.setIsDisabled(false);

        baseMapper.insert(ucenterMember);

    }

    @Override
    public UcenterMember getByOpenid(String openid) {

        QueryWrapper<UcenterMember> ucenterMemberQueryWrapper = new QueryWrapper<>();

        ucenterMemberQueryWrapper.eq("openid",openid);

        UcenterMember ucenterMember = baseMapper.selectOne(ucenterMemberQueryWrapper);

        return ucenterMember;
    }

    @Override
    public Integer getRegisterCount(String day) {
        return baseMapper.getRegisterCount(day);
    }
}
